/*
 * Copyright 2009 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.android.apps.mytracks.casos;

import com.doverty.android.maps.mytracks.R;
import com.google.android.apps.mytracks.AbstractMyTracksActivity;
import com.google.android.apps.mytracks.clientes.ClienteDataSource;
import com.google.android.apps.mytracks.content.Caso;
import com.google.android.apps.mytracks.content.Investigado;
import com.google.android.apps.mytracks.fragments.DeleteCasoDialogFragment.DeleteCasoCaller;
import com.google.android.apps.mytracks.investigados.InvestigadoDataSource;

import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import java.util.List;

/**
 * An activity to display caso detail info.
 * 
 * @author Leif Hendrik Wilden
 */
public class CasoDetailActivity extends AbstractMyTracksActivity implements DeleteCasoCaller {

  public static final String EXTRA_CASO_ID = "caso_id";
  private static final String TAG = CasoDetailActivity.class.getSimpleName();

  private Caso caso;
  private CasoDataSource dbCaso;
  private ClienteDataSource dbCliente;
  private InvestigadoDataSource dbInvestigado;
  private List<Investigado> investigadosCaso;
  private TextView casoNombre;
  private TextView cliente;
  private TextView investigado1;
  private TextView investigado2;
  private TextView investigado3;
  private TextView fechaComienzo;
  private TextView fechaFin;

  @Override
  protected void onCreate(Bundle bundle) {
    super.onCreate(bundle);

    long casoId = getIntent().getLongExtra(EXTRA_CASO_ID, -1L);
    if (casoId == -1L) {
      Log.d(TAG, "invalid caso id");
      finish();
      return;
    }

    dbCaso = new CasoDataSource(this);
    dbCaso.open();
    dbCliente = new ClienteDataSource(this);
    dbCliente.open();
    dbInvestigado = new InvestigadoDataSource(this);
    dbInvestigado.open();
    
    caso = dbCaso.getCaso(casoId);
    investigadosCaso = dbInvestigado.getAllInvestigadosCaso(casoId);
    casoNombre = (TextView) findViewById(R.id.caso_detail_nombre_info);
    casoNombre.setText(caso.getNombre());
    cliente = (TextView) findViewById(R.id.caso_detail_cliente_info);
    cliente.setText(dbCliente.getCliente(caso.getClienteId()).getNombre());

    if (investigadosCaso.size()>0){
      investigado1 = (TextView) findViewById(R.id.caso_detail_investigado_info1);
      investigado1.setText(investigadosCaso.get(0).getNombre());
      if (investigadosCaso.size()>1){
        investigado2 = (TextView) findViewById(R.id.caso_detail_investigado_info2);
        investigado2.setText(investigadosCaso.get(1).getNombre());
        if (investigadosCaso.size()>2){
          investigado3 = (TextView) findViewById(R.id.caso_detail_investigado_info3);
          investigado3.setText(investigadosCaso.get(2).getNombre());
        }
      }
    }

    fechaComienzo = (TextView) findViewById(R.id.caso_detail_fecha_comienzo_info);
    fechaComienzo.setText(caso.getFechaInicio());
    fechaFin = (TextView) findViewById(R.id.caso_detail_fecha_fin_info);
    fechaFin.setText(caso.getFechaFin());
  }

  @Override
  protected int getLayoutResId() {
    return R.layout.caso_detail_activity;
  }

  @Override
  public void onDeleteCasoDone() {
    runOnUiThread(new Runnable() {
        @Override
      public void run() {
        finish();
      }
    });
  }

}